package com.ruoyi.sale.service.impl;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import com.alibaba.csp.sentinel.util.AssertUtil;
import com.ruoyi.common.core.utils.CodeGenerateUtils;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.sale.domain.*;
import com.ruoyi.sale.mapper.*;
import com.ruoyi.sale.vo.SaleStoreoutVo;
import com.ruoyi.system.api.model.LoginUser;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.sale.service.ISaleStoreoutService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 销售出库Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-09-03
 */
@Service
public class SaleStoreoutServiceImpl implements ISaleStoreoutService 
{
    @Autowired
    private SaleStoreoutMapper saleStoreoutMapper;
    @Autowired
    private SysAccountMapper accountMapper;
    @Autowired
    private SysCustomerMapper customerMapper;
    @Autowired
    private SaleStoreoutDetailMapper saleStoreoutDetailMapper;
    @Autowired
    private SalePayorderMapper salePayorderMapper;
    @Autowired
    private SaleOrderMapper saleOrderMapper;
    @Autowired
    private SaleOrderDetailMapper saleOrderDetailMapper;
    @Autowired
    private SysStoreMapper sysStoreMapper;

    /**
     * 查询销售出库
     * 
     * @param id 销售出库主键
     * @return 销售出库
     */
    @Override
    public SaleStoreout selectSaleStoreoutById(Long id)
    {
        return saleStoreoutMapper.selectSaleStoreoutById(id);
    }

    /**
     * 查询销售出库列表
     * 
     * @param saleStoreout 销售出库
     * @return 销售出库
     */
    @Override
    public List<SaleStoreout> selectSaleStoreoutList(SaleStoreout saleStoreout)
    {
        return saleStoreoutMapper.selectSaleStoreoutList(saleStoreout);
    }

    /**
     * @description: 新增销售出库单
     * @param: storeoutVo
     * @return: com.ruoyi.common.core.web.domain.AjaxResult
     * @author clqlongbao
     * @date: 2022/9/3 1:15
     */
    @Override
    @Transactional
    public int insertSaleStoreout(SaleStoreoutVo storeoutVo){
        Date date = new Date();
        //获取当前登陆人
        LoginUser user = SecurityUtils.getLoginUser();
        System.out.println(user.toString());
        //出库单和销售单使用同一个订单号
        Long orderNo = storeoutVo.getSelectSaleOrderList().get(0).getOrderNo();
        System.out.println("销售订单号为："+orderNo);
        //保存出库主订单
        SaleStoreout saleStoreout = new SaleStoreout();
        BeanUtils.copyProperties(storeoutVo,saleStoreout);
        saleStoreout.setOrderNo(orderNo);
        saleStoreout.setCreateBy(user.getUsername());
        saleStoreout.setCreateTime(date);
        //结算账户
        SysAccount account = accountMapper.selectSysAccountById(storeoutVo.getAccountId());
        saleStoreout.setAccountName(account.getName());
        saleStoreout.setCustomerName(customerMapper.selectSysCustomerById(storeoutVo.getCustomerId()).getName());
        List<SaleOrder> saleOrderList = storeoutVo.getSelectSaleOrderList();
        String productName = saleOrderList.stream().map(SaleOrder::getProductName).collect(Collectors.toList()).toString();
        System.out.println("商品名称："+productName);
        saleStoreout.setProductName(productName);
        saleStoreoutMapper.insertSaleStoreout(saleStoreout);
        System.out.println("出库主订单保存成功");
        //保存出库子订单
        saleOrderList.forEach(saleOrder->{
            SaleStoreoutDetail saleStoreoutDetail = new SaleStoreoutDetail();
            BeanUtils.copyProperties(saleOrder,saleStoreoutDetail);
            saleStoreoutDetail.setId(null);
            saleStoreoutDetail.setAccount(account.getName());
            saleStoreoutDetail.setOrderNo(orderNo);
            saleStoreoutDetail.setCreateTime(date);
            saleStoreoutDetailMapper.insertSaleStoreoutDetail(saleStoreoutDetail);
            System.out.println("出库明细订单保存成功");
        });
        //封装支付单
        SalePayorder salePayorder = new SalePayorder();
        salePayorder.setOrderNo(orderNo);
        salePayorder.setAccountId(storeoutVo.getAccountId());
        salePayorder.setProductName(productName);
        salePayorder.setAccountName(account.getName());
        salePayorder.setAmount(storeoutVo.getAmount());
        salePayorder.setEarnest(storeoutVo.getEarnest());
        salePayorder.setPay(storeoutVo.getPay());
        salePayorder.setReduced(storeoutVo.getReduced());
        salePayorder.setUserId(user.getUserid());
        salePayorder.setCreateTime(date);
        salePayorderMapper.insertSalePayorder(salePayorder);
        System.out.println("出库支付单保存成功");
        //模拟支付
        account.setBalance(account.getBalance()+storeoutVo.getPay());
        account.setUpdateTime(date);
        accountMapper.updateSysAccount(account);
        System.out.println("账户增加尾款成功");
        //扣减库存
        //1.找到商品的库存信息
        //2.找到商品的数量
        for (SaleOrder saleOrder : saleOrderList) {
            List<SaleOrderDetail> saleOrderDetails = saleOrderDetailMapper.selectSaleOrderdetailByOrderNo(saleOrder.getOrderNo());
            saleOrderDetails.forEach(saleOrderDetail -> {
                //得到该商品的库存信息
                SysStore sysStore=sysStoreMapper.selectSysStoreByproductName(saleOrderDetail.getProductName());
                //扣减数量
                Integer integer = Integer.valueOf(sysStore.getNumber());
                int i = integer - saleOrderDetail.getNumber();
                sysStore.setNumber(Integer.valueOf(i).toString());
                sysStore.setUpdateBy(user.getUsername());
                sysStore.setUpdateTime(date);
                sysStoreMapper.updateSysStore(sysStore);
            });
        }
        System.out.println("扣减库存成功");

        return 1;
    }

    /**
     * 修改销售出库
     * 
     * @param saleStoreout 销售出库
     * @return 结果
     */
    @Override
    public int updateSaleStoreout(SaleStoreout saleStoreout)
    {
        saleStoreout.setUpdateTime(DateUtils.getNowDate());
        return saleStoreoutMapper.updateSaleStoreout(saleStoreout);
    }

    /**
     * 批量删除销售出库
     * 
     * @param ids 需要删除的销售出库主键
     * @return 结果
     */
    @Override
    public int deleteSaleStoreoutByIds(Long[] ids)
    {
        return saleStoreoutMapper.deleteSaleStoreoutByIds(ids);
    }

    /**
     * 删除销售出库信息
     * 
     * @param id 销售出库主键
     * @return 结果
     */
    @Override
    public int deleteSaleStoreoutById(Long id)
    {
        return saleStoreoutMapper.deleteSaleStoreoutById(id);
    }

    /**
     * @description: 根据客户id查询已经出库的销售单
     * @param: id
     * @return: com.ruoyi.common.core.web.domain.AjaxResult
     * @author clqlongbao
     * @date: 2022/9/3 9:45
     */
    @Override
    public List<SaleStoreout> getStoreoutByCustomerId(Long id) {
        return saleStoreoutMapper.getStoreoutByCustomerId(id);
    }
}
